<?php
/**
 * Created by PhpStorm.
 * User: wuanjie
 * Date: 16-4-5
 * Time: 下午2:48
 */
class WeixinMeterialApi {

    const API_GET = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s';
    const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s';
    const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s';
    const API_STATS = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s';
    const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s';
    const API_NEWS_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s';
    const API_NEWS_UPDATE = 'https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=%s';
    const API_NEWS_IMAGE_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s';

    /**
     * 添加临时素材
     * @param $path 素材路径
     * @param $type 素材类型
     * @return mixed
     */
    function addTempMeterial($path, $type) {

        $token = Tools_help::getToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$token&type=$type";
        $requestData = array(
            'media' => '@' . $path
        );
        $result = Tools_help::weixinCurlPost($url, $requestData);
        return json_decode($result);
    }

    /**
     * 添加永久图片素材
     * @param $path 图片素材路径
     * @return mixed
     */
    function addForeverImage($path) {
        $requestData = array(
            'media' => "@" . $path
        );
        return $this->access(self::API_UPLOAD, $requestData);
    }

    /**
     * 添加永久语音素材
     * @param $path 语音素材路径
     * @return mixed
     */
    function addForeverVoice($path) {
        $requestData = array(
            'media' => "@" . $path
        );
        return $this->access(self::API_UPLOAD, $requestData);
    }

    /**
     * 添加永久视频素材
     * @param $path 视频路径
     * @param $title 视频标题
     * @param $introduction 视频简介
     * @return mixed
     */
    function addForeverVideo($path, $title, $introduction) {
        $requestData = array(
            'media' => "@" . $path,
            'description' => json_encode([
                'title' => $title,
                'introduction' => $introduction
            ])
        );
        return $this->access(self::API_UPLOAD, $requestData);
    }

    /**
     * 添加永久缩略图素材
     * @param $path 缩略图路径
     * @return mixed
     */
    function addForeverThumb($path) {
        $requestData = array(
            'media' => "@" . $path
        );
        return $this->access(self::API_UPLOAD, $requestData);
    }

    /**
     * 添加图文素材
     * @param $data 图文数据
     * @return mixed
     */
    function addArticles($data) {
        $tempStart = "{
          \"articles\": [";
        $temp = "{
               \"title\": %s,
               \"thumb_media_id\": %s,
               \"author\": %s,
               \"digest\": %s,
               \"show_cover_pic\": %s,
               \"content\": %s,
               \"content_source_url\": %s
            },
         ";
        $tempContent = "";
        $tempEnd = "]
            }";
        foreach ($data as $value) {
            $tempContent .= sprintf($temp, $value['title'], $value['thumb_media_id'], $value['author'], $value['digest']
                , $value['show_cover_pic'], $value['content'], $value['content_source_url']);
        }
        $requestData = $tempStart . $tempContent . $tempEnd;
        $result = $this->access($this::API_NEWS_UPLOAD, $requestData);
        return $result;
    }

    /**
     * 在图文消息的具体内容中，将过滤外部的图片链接。可通过下述接口上传得到url，放到图文消息的具体内容中使用
     * @param $path
     * @return mixed
     */
    function addAricleImage($path) {
        $requestData = array(
            'media' => "@" . $path
        );
        return $this->access(self::API_NEWS_IMAGE_UPLOAD, $requestData);
    }

    /**
     * 修改指定图文素材
     * @param $media 指定图文素材的media_id
     * @param $index 要更新的文章在图文消息中的位置（多图文消息时，此字段才有意义），第一篇为0
     * @param $data 更新的数据
     * @return mixed
     */
    function updateArticle($media, $index, $data) {
        $temp = "{
          \"media_id\": %s,
          \"index\": %s,
          \"articles\": {
               \"title\": %s,
               \"thumb_media_id\": %s,
               \"author\": %s,
               \"digest\": %s,
               \"show_cover_pic\": %s,
               \"content\": %s,
               \"content_source_url\": %s
            }
        }";
        $requestData = sprintf($temp, $media, $index, $data['title'], $data['thumb_media_id'], $data['author']
            , $data['digest'], $data['show_cover_pic'], $data['content'], $data['content_source_url']);
        return $this->access(self::API_NEWS_UPDATE, $requestData);
    }
    /**
     * 删除指定素材
     * @param $media 删除指定素材的media_id
     * @return mixed
     */
    function deleteMeterial($media) {
        $requestData = array(
            'media_id' => $media
        );
        return $this->$this->access(self::API_DELETE, $requestData);
    }

    //获取素材总数
    function getMeterialCount() {
        return $this->access(self::API_STATS);
    }

    /**
     * 获取指定素材
     * @param $media 指定素材的media_id
     * @return mixed
     */
    function getMeterial($media) {
        $requestData = array(
            'media_id' => $media
        );
        return $this->access(self::API_GET, $requestData);
    }
    /**
     * 获取素材列表
     * @param $type 获取素材的类型
     * @param $offset 素材总列表的偏移量
     * @param $count 获取素材的总数，取值在1到20之间
     * @return mixed
     */
    function getMeterials($type, $offset, $count) {
        $requestData = array(
            "type" => $type,
            "offset" => $offset,
            "count" => $count
        );
        return $this->access(self::API_LISTS, $requestData);
    }

    /**
     * 连接微信后台
     * @param $api 接口
     * @param string $requestData 请求数据
     * @return mixed
     */
    function access($api, $requestData = '') {
        $token = Tools_help::getToken();
        $url = sprintf($api, $token);
        $result = Tools_help::weixinCurlPost($url, $requestData);
        $object = json_decode($result);
        return $object;
    }
}
